'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  //list页面热点 渲染。1
  async List() {
    const { ctx, app } = this;
    const data = await app.mysql.select('list')
    // console.log(data);
    ctx.body = {
      code: 200,
      data,
    }
  }


  //list页面 搜索1
  async Listsearch() {
    const { ctx, app } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.query(`select * from list where title like '%${value}%'`)
    // console.log(value);
    ctx.body = {
      code: 200,
      msg: "搜索成功",
      data,
    }
  }


  //list页面话题  home页面渲染
  async Homelist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('homelist')
    ctx.body = {
      code: 200,
      data,
    }
  }




  //shop页面 美洽心选 渲染
  async Shoplist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('shoplist')
    ctx.body = {
      code: 200,
      data,
    }
  }


  //购物车
  async shopcarlist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('shopcar')
    ctx.body = {
      code: '000',
      data
    }
  }
  //添加到购物车
  async addcar() {
    const { ctx, app } = this;
    const { id, title, img, price, num } = ctx.request.body;

    const data = await app.mysql.get('shopcar', { id, title, img, price })
    //判断是否有该数据
    if (data) {
      //如果有数量+1
      await app.mysql.update('shopcar', { num: data.num + 1, id })
    } else {
      //如果没有添加一条
      await app.mysql.insert('shopcar', { num: num + 1, title, img, price, id, flag: 0 })
    }
    ctx.body = {
      code: '000',
      msg: '请求成功'
    }
  }
  //删除
  async delcar() {
    const { ctx, app } = this
    const { id } = ctx.request.body;
    const data = await app.mysql.delete('shopcar', { id })
    ctx.body = {
      data,
      code: '000'
    }
  }
  //++ --
  async changecar() {
    const { ctx, app } = this
    const { value, id } = ctx.request.body;
    console.log(value);
    const data = await app.mysql.update('shopcar', { num: value, id })
    ctx.body = {
      data,
      code: '000'
    }
  }


  // //单选
  // async exitflag() {
  //   const { ctx, app } = this
  //   const { id, flag } = ctx.request.body;
  //   console.log(!!flag);

  //   if (flag === 0) {
  //     await app.mysql.update('shopcar', { id, flag: 1 })
  //     ctx.body = {
  //       data: await app.mysql.select('shopcar', { id }),
  //       code: '000'
  //     }

  //   } else if (flag === 1) {
  //     const data = await app.mysql.update('shopcar', { id, flag: 0 })
  //     ctx.body = {
  //       data,
  //       code: '001'
  //     }
  //     console.log(data);
  //   }
  // }
  // //全选
  // async checkall() {
  //   const { ctx, app } = this
  //   await app.mysql.UPDATE(`shopcar SET flag = '1'`)
  //   // await app.mysql.query(`UPDATE shopcar  SET flag = '1' `)
  // }
  //添加到去支付
  async addpayment() {
    const { ctx, app } = this;
    const { id, title, img, price, num } = ctx.request.body;

    const data = await app.mysql.get('order', { id, title, img, price })
    //判断是否有该数据
    if (data) {
      //如果有数量+1
      await app.mysql.update('order', { num: data.num + 1, id })
    } else {
      //如果没有添加一条
      await app.mysql.insert('order', { num: num + 1, title, img, price, id, flag: 0 })
    }
    ctx.body = {
      code: '000',
      msg: '请求成功'
    }
  }


  //订单
  async orderlist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('order')
    ctx.body = {
      code: '000',
      data
    }
  }

  //地址页面渲染
  async address() {
    const { ctx, app } = this;
    const data = await app.mysql.select('address')
    ctx.body = {
      code: 200,
      data,
    }
  }


}

module.exports = HomeController;
